home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Gigarom 1
/
Gigarom Macintosh Archives (Quantum Leap)(CDRM1080320)(1993).iso
/
FILES
/
DEV
/
C-H
/
CMacPrimer.cpt
/
Zinger ƒ
/
zinger.c
< prev
Wrap
C/C++ Source or Header
|
1990-03-18
|
5KB
|
219 lines
#define BASE_RES_ID 400
#define NIL_POINTER 0L
#define MOVE_TO_FRONT -1L
#define REMOVE_ALL_EVENTS 0
#define MIN_SLEEP 0L
#define NIL_MOUSE_REGION 0L
#define DRAG_THRESHOLD 30
#define WNE_TRAP_NUM 0x60
#define UNIMPL_TRAP_NUM 0x9F
#define POPUP_MENU_ID BASE_RES_ID
#define NOT_A_NORMAL_MENU -1
#define POPUP_LEFT 100
#define POPUP_TOP 35
#define POPUP_RIGHT 125
#define POPUP_BOTTOM 52
#define SHADOW_PIXELS 1
#define RIGHT_MARGIN 5
#define BOTTOM_MARGIN 4
#define LEFT_MARGIN 5
#define PIXEL_FOR_TOP_LINE 1
Boolean gDone, gWNEImplemented;
int gPopUpItem = 1, gPopUpLabelWidth;
MenuHandle gPopUpMenu;
EventRecord gTheEvent;
Rect gPopUpRect, gLabelRect, gDragRect;
/****************** Main ****************************/
main()
{
ToolBoxInit();
WindowInit();
SetUpDragRect();
MenuBarInit();
DrawPopUp();
MainLoop();
}
/**************** ToolBoxInit ***********************/
ToolBoxInit()
{
InitGraf (&thePort);
InitFonts();
FlushEvents (everyEvent, REMOVE_ALL_EVENTS);
InitWindows();
InitMenus();
TEInit();
InitDialogs(NIL_POINTER);
InitCursor();
}
/************************* WindowInit ****************/
WindowInit ()
{
WindowPtr popUpWindow;
popUpWindow = GetNewWindow (BASE_RES_ID, NIL_POINTER, MOVE_TO_FRONT);
ShowWindow(popUpWindow);
SetPort(popUpWindow);
TextFont(systemFont);
TextMode(srcCopy);
}
/******************* SetUpDragRect **********************/
SetUpDragRect()
{
gDragRect = screenBits.bounds;
gDragRect.left += DRAG_THRESHOLD;
gDragRect.right -= DRAG_THRESHOLD;
gDragRect.bottom -= DRAG_THRESHOLD;
}
/************** MenuBarInit ******************************/
MenuBarInit()
{
gPopUpMenu = GetMenu (POPUP_MENU_ID);
InsertMenu(gPopUpMenu, NOT_A_NORMAL_MENU);
HLock(gPopUpMenu);
gPopUpLabelWidth = StringWidth ((**gPopUpMenu).menuData);
HUnlock(gPopUpMenu);
}
/*************** DrawPopUp ********************************/
DrawPopUp()
{
SetRect (&gPopUpRect, POPUP_LEFT,POPUP_TOP,POPUP_RIGHT,POPUP_BOTTOM);
FrameRect (&gPopUpRect);
MoveTo (gPopUpRect.left+SHADOW_PIXELS, gPopUpRect.bottom);
LineTo (gPopUpRect.right, gPopUpRect.bottom);
LineTo (gPopUpRect.right, gPopUpRect.top+SHADOW_PIXELS);
MoveTo (gPopUpRect.left - gPopUpLabelWidth - RIGHT_MARGIN,
gPopUpRect.bottom - BOTTOM_MARGIN);
HLock(gPopUpMenu);
DrawString ((**gPopUpMenu).menuData);
HUnlock(gPopUpMenu);
gLabelRect.top = gPopUpRect.top + PIXEL_FOR_TOP_LINE;
gLabelRect.left = gPopUpRect.left - gPopUpLabelWidth
- LEFT_MARGIN - RIGHT_MARGIN;
gLabelRect.right = gPopUpRect.left;
gLabelRect.bottom = gPopUpRect.bottom;
DrawPopUpNumber();
}
/********************** DrawPopUpNumber ***********************/
DrawPopUpNumber()
{
Str255 menuItem;
int itemLeftMargin;
GetItem (gPopUpMenu, gPopUpItem, &menuItem);
itemLeftMargin = (gPopUpRect.right - gPopUpRect.left -
StringWidth(menuItem))/2;
MoveTo (gPopUpRect.left + itemLeftMargin,
gPopUpRect.bottom - BOTTOM_MARGIN);
DrawString(menuItem);
}
/******************* MainLoop ******************************/
MainLoop()
{
gDone = FALSE;
gWNEImplemented = (NGetTrapAddress(WNE_TRAP_NUM, ToolTrap) !=
NGetTrapAddress(UNIMPL_TRAP_NUM, ToolTrap));
while(gDone == FALSE)
{
HandleEvent();
}
}
/*******************HandleEvent***************/
HandleEvent()
{
if(gWNEImplemented)
WaitNextEvent (everyEvent, &gTheEvent,MIN_SLEEP,
NIL_MOUSE_REGION);
else
{
SystemTask();
GetNextEvent(everyEvent,&gTheEvent);
}
switch (gTheEvent.what)
{
case mouseDown:
HandleMouseDown();
break;
case updateEvt:
BeginUpdate(gTheEvent.message);
DrawPopUp();
EndUpdate(gTheEvent.message);
break;
}
}
/********************** HandleMouseDown ***********************/
HandleMouseDown()
{
WindowPtr whichWindow;
short int thePart, i;
long int theChoice;
Point myPoint, popUpUpperLeft;
thePart = FindWindow (gTheEvent.where, &whichWindow);
switch(thePart)
{
case inContent:
myPoint = gTheEvent.where;
GlobalToLocal (&myPoint);
if ( PtInRect(myPoint,&gPopUpRect))
{
InvertRect(&gLabelRect);
popUpUpperLeft.v = gPopUpRect.top +PIXEL_FOR_TOP_LINE;
popUpUpperLeft.h = gPopUpRect.left;
LocalToGlobal(&popUpUpperLeft);
theChoice = PopUpMenuSelect(gPopUpMenu,
popUpUpperLeft.v, popUpUpperLeft.h,
gPopUpItem);
InvertRect (&gLabelRect);
if (LoWord(theChoice)>0)
{
gPopUpItem = LoWord(theChoice);
DrawPopUpNumber();
for(i=0;i<gPopUpItem;i++)
SysBeep(20);
}
}
break;
case inSysWindow:
SystemClick(&gTheEvent,whichWindow);
break;
case inDrag:
DragWindow(whichWindow,gTheEvent.where,&gDragRect);
break;
case inGoAway:
gDone = TRUE;
break;
}
}